package com.example.a2021_10_08;

import androidx.appcompat.app.AppCompatActivity;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;



public class MainActivity extends AppCompatActivity implements  View.OnClickListener{

    private weixinFragment  weixinFragment = new weixinFragment();
    private friendFragment friendFragment = new friendFragment();
    private contactFragment contactFragment = new contactFragment();
    private configFragment configFragment = new configFragment();
    private FragmentManager fragmentManager;
    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView3;
    private ImageView imageView4;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        linearLayout1 = findViewById(R.id.linearlayout_weixin);
        linearLayout2 = findViewById(R.id.linearlayout_friend);
        linearLayout3 = findViewById(R.id.linearlayout_contact);
        linearLayout4 = findViewById(R.id.linearlayout_config);
        imageView1 = findViewById(R.id.imageview_weixin);
        imageView2 = findViewById(R.id.imageview_friend);
        imageView3 = findViewById(R.id.imageview_contact);
        imageView4 = findViewById(R.id.imageview_config);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);


        initFragment();


    }

    private void initFragment(){
        fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.id_content,weixinFragment);
        transaction.add(R.id.id_content,friendFragment);
        transaction.add(R.id.id_content,contactFragment);
        transaction.add(R.id.id_content,configFragment);
        hideFragment(transaction);
        transaction.show(weixinFragment);
        imageView1.setImageResource(R.drawable.green);
        transaction.commit();
    }

    private  void hideFragment(FragmentTransaction transaction){
        transaction.hide(weixinFragment);
        transaction.hide(friendFragment);
        transaction.hide(contactFragment);
        transaction.hide(configFragment);

    }

    private void showfragment(int i){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch(i){
            case 0:
                transaction.show(weixinFragment);
                emptyimage();
                imageView1.setImageResource(R.drawable.green);
                break;
            case 1:
                transaction.show(friendFragment);
                emptyimage();
                imageView2.setImageResource(R.drawable.green);
                break;
            case 2:
                transaction.show(contactFragment);
                emptyimage();
                imageView3.setImageResource(R.drawable.green);
                break;
            case 3:
                transaction.show(configFragment);
                emptyimage();
                imageView4.setImageResource(R.drawable.green);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    public void emptyimage(){
        imageView1.setImageResource(R.drawable.blank);
        imageView2.setImageResource(R.drawable.blank);
        imageView3.setImageResource(R.drawable.blank);
        imageView4.setImageResource(R.drawable.blank);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linearlayout_weixin:
                showfragment(0);
                break;
            case R.id.linearlayout_friend:
                showfragment(1);
                break;
            case R.id.linearlayout_contact:
                showfragment(2);
                break;
            case R.id.linearlayout_config:
                showfragment(3);
                break;
            default:
                break;
        }
    }




}